package com.ybg.bookmanages.controller;

import com.ybg.bookcommons.dto.BorrowBookDTO;
import com.ybg.bookcommons.dto.ReturnDTO;
import com.ybg.bookmanages.module.entity.Bookinfos;
import com.ybg.bookmanages.module.vo.BookinfosVO;
import com.ybg.bookmanages.services.BookinfosServcie;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.ZoneId;
import java.util.Date;

@RestController
@RequestMapping("/manage")
public class ManagerCtrl {

    @Resource
    private BookinfosServcie bookinfosServcie;
    /**
     * 将用户传来的时间 按照格式封装成java.util.Date
     * @param wdb
     */
    @InitBinder
    public void dateBinder(WebDataBinder wdb){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        wdb.registerCustomEditor(Date.class,new CustomDateEditor(sdf,true));
    }

    /**
     * 管理员录入一本新书 包含书的图片 文件上传
     * @param book 书的信息
     * @param file 图片文件
     * @return
     */
    @RequestMapping(value = "/addbook",method = RequestMethod.POST)
    public ReturnDTO addBook(BookinfosVO book,
                             MultipartFile file){

        bookinfosServcie.addBook(book,file);
        return ReturnDTO.builder().status("success").message("新增图书成功").build();
    }

    /**
     * 根据用户输入的图书isbn对该书进行库存-1操作 并判断如果库存为0 则下架
     * @param isbn
     * @param reason
     * @return
     */
    @RequestMapping("/bookshutdown")
    public ReturnDTO shut(
            String isbn,
            String reason
    ){
        boolean res = bookinfosServcie.damageBook(isbn, reason);
        if(res){
            return ReturnDTO.builder()
                    .status("success")
                    .message("图书破损操作成功")
                    .build();
        }else{
            return ReturnDTO.builder()
                    .status("fail")
                    .message("图书破损无效")
                    .build();
        }

    }

    /**
     * 借阅图书
     * @param isbn
     * @return
     */
    @RequestMapping("/borrow/{isbn}/{cardid}")
    public ReturnDTO borrow(@PathVariable String isbn,@PathVariable String cardid){
        boolean res = bookinfosServcie.borrowBook(isbn, cardid);
        if(res){
            return ReturnDTO.builder()
                    .status("success")
                    .message("借阅成功")
                    .build();
        }else{
            return ReturnDTO.builder()
                    .status("fail")
                    .message("图书库存不足,借阅失败")
                    .build();
        }

    }

    /**
     * 用户归还图书
     * @param isbn
     * @return
     */
    @RequestMapping("/back/{isbn}/{cardid}")
    public BorrowBookDTO back(@PathVariable String isbn,@PathVariable String cardid){
        BorrowBookDTO bbdto = bookinfosServcie.backBook(isbn, cardid);
        return bbdto;
    }
}
